<!--<template>
  <el-card class="planting-suggestions-container">
    <h1 class="title">种植意见大全</h1>
    <el-collapse v-model="activeNames">
      <el-collapse-item v-for="(item, index) in plantingSuggestions" :key="index" :title="item.title" :name="index">
        <div>{{ item.content }}</div>
      </el-collapse-item>
    </el-collapse>
  </el-card>
</template>-->

<!--<script>
export default {
  data() {
    return {
      activeNames: [], // 当前展开的面板
      plantingSuggestions: [
        {
          title: "1. 选择合适的土壤类型",
          content: "根据作物需求选择疏松、肥沃、排水良好的土壤，避免重黏土或沙质土壤。"
        },
        {
          title: "2. 合理轮作种植",
          content: "通过轮作减少病虫害发生，提高土壤肥力，例如豆科作物与禾本科作物轮作。"
        },
        {
          title: "3. 适时播种",
          content: "根据当地气候条件选择最佳播种时间，避免过早或过晚播种影响作物生长。"
        },
        {
          title: "4. 科学施肥",
          content: "根据土壤检测结果和作物需求，合理施用氮、磷、钾等肥料，避免过量施肥。"
        },
        {
          title: "5. 定期灌溉",
          content: "根据作物生长阶段和天气情况，制定合理的灌溉计划，保持土壤湿润但不过湿。"
        },
        {
          title: "6. 病虫害防治",
          content: "采用生物防治和化学防治相结合的方式，定期检查作物健康状况，及时处理病虫害。"
        },
        {
          title: "7. 使用覆盖物",
          content: "在土壤表面覆盖秸秆或塑料薄膜，减少水分蒸发，抑制杂草生长。"
        },
        {
          title: "8. 选择抗病品种",
          content: "优先选择抗病虫害能力强的作物品种，减少农药使用，提高产量。"
        },
        {
          title: "9. 合理密植",
          content: "根据作物特性调整种植密度，避免过密导致通风不良或过疏浪费土地资源。"
        },
        {
          title: "10. 定期修剪",
          content: "对果树或灌木类作物进行定期修剪，促进通风透光，提高果实品质。"
        },
        {
          title: "11. 利用有机肥料",
          content: "使用堆肥或农家肥替代部分化学肥料，改善土壤结构，提高土壤有机质含量。"
        },
        {
          title: "12. 种植绿肥作物",
          content: "在休耕期种植紫云英、苜蓿等绿肥作物，翻压后增加土壤养分。"
        },
        {
          title: "13. 避免连作障碍",
          content: "长期种植同一种作物会导致土壤养分失衡，建议轮作或间作其他作物。"
        },
        {
          title: "14. 采用滴灌技术",
          content: "滴灌可以节约水资源，精准供水，减少水分蒸发和土壤板结。"
        },
        {
          title: "15. 种植防风林",
          content: "在农田周围种植防风林，减少风蚀，改善小气候环境。"
        },
        {
          title: "16. 定期土壤检测",
          content: "每年进行一次土壤检测，了解土壤养分状况，调整施肥方案。"
        },
        {
          title: "17. 利用天敌防治害虫",
          content: "引入瓢虫、寄生蜂等天敌，减少化学农药使用，保护生态环境。"
        },
        {
          title: "18. 种植耐旱作物",
          content: "在干旱地区选择耐旱作物品种，如高粱、谷子等，提高种植成功率。"
        },
        {
          title: "19. 采用无土栽培技术",
          content: "在资源有限的地区尝试无土栽培，利用营养液提供作物所需养分。"
        },
        {
          title: "20. 加强田间管理",
          content: "定期除草、松土、追肥，保持田间清洁，减少病虫害滋生环境。"
        }
      ]
    };
  }
};
</script>-->

<!--<style scoped>
.planting-suggestions-container {
  width: 1000px;
  margin: 20px auto;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #303133;
}

.el-collapse-item {
  margin-bottom: 10px;
}
</style>-->

<template>
  <div id="app">
    <el-card>
      <template #header>
        <h1>种植经验管理</h1>
      </template>
      <el-button type="primary" @click="openAddDialog">新增种植经验</el-button>
      <el-table :data="experienceList" stripe @row-dblclick="openEditDialog">
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="content" label="内容"></el-table-column>
        <el-table-column prop="time" label="时间"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="warning" size="small" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="deleteExperience(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :visible.sync="addDialogVisible" title="新增种植经验">
      <template #content>
        <el-form :model="newExperience" ref="addFormRef" label-width="80px">
          <el-form-item label="标题">
            <el-input v-model="newExperience.title"></el-input>
          </el-form-item>
          <el-form-item label="内容">
            <el-input v-model="newExperience.content" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="时间">
            <el-date-picker
                v-model="newExperience.time"
                type="datetime"
                placeholder="选择日期和时间"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addExperience">保存</el-button>
      </template>
    </el-dialog>
    <el-dialog :visible.sync="editDialogVisible" title="编辑种植经验">
      <template #content>
        <el-form :model="editedExperience" ref="editFormRef" label-width="80px">
          <el-form-item label="标题">
            <el-input v-model="editedExperience.title"></el-input>
          </el-form-item>
          <el-form-item label="内容">
            <el-input v-model="editedExperience.content" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="时间">
            <el-date-picker
                v-model="editedExperience.time"
                type="datetime"
                placeholder="选择日期和时间"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateExperience">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

const experienceList = ref([
  {
    id: 1,
    title: '番茄种植经验',
    content: '番茄喜欢充足的阳光和肥沃的土壤……',
    time: '2024-10-01 12:00:00'
  }
]);
const addDialogVisible = ref(false);
const editDialogVisible = ref(false);
const newExperience = ref({
  title: '',
  content: '',
  time: ''
});
const editedExperience = ref({
  id: null,
  title: '',
  content: '',
  time: ''
});
const addFormRef = ref(null);
const editFormRef = ref(null);

const openAddDialog = () => {
  addDialogVisible.value = true;
  newExperience.value = {
    title: '',
    content: '',
    time: ''
  };
};

const openEditDialog = (item) => {
  editDialogVisible.value = true;
  editedExperience.value = { ...item };
};

const addExperience = () => {
  if (!newExperience.value.title || !newExperience.value.content || !newExperience.value.time) {
    ElMessage.warning('请填写完整信息');
    return;
  }
  const newId = experienceList.value.length > 0 ? experienceList.value[experienceList.value.length - 1].id + 1 : 1;
  newExperience.value.id = newId;
  experienceList.value.push({ ...newExperience.value });
  addDialogVisible.value = false;
  ElMessage.success('新增成功');
};

const updateExperience = () => {
  if (!editedExperience.value.title || !editedExperience.value.content || !editedExperience.value.time) {
    ElMessage.warning('请填写完整信息');
    return;
  }
  const index = experienceList.value.findIndex(item => item.id === editedExperience.value.id);
  if (index!== -1) {
    experienceList.value[index] = { ...editedExperience.value };
    editDialogVisible.value = false;
    ElMessage.success('编辑成功');
  }
};

const deleteExperience = (item) => {
  const index = experienceList.value.findIndex(exp => exp.id === item.id);
  if (index!== -1) {
    experienceList.value.splice(index, 1);
    ElMessage.success('删除成功');
  }
};
</script>

<style scoped>
h1 {
  font-size: 24px;
  margin-bottom: 20px;
}

.el-button {
  margin-bottom: 20px;
}
</style>

